package top.codervan.yygh.hosp.controller.api;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.codervan.yygh.common.exception.YyghException;
import top.codervan.yygh.common.result.Result;
import top.codervan.yygh.common.result.ResultCodeEnum;
import top.codervan.yygh.hosp.service.DepartmentService;
import top.codervan.yygh.hosp.service.HospitalService;
import top.codervan.yygh.hosp.service.HospitalSetService;
import top.codervan.yygh.hosp.service.ScheduleService;
import top.codervan.yygh.hosp.utils.HospApiUtil;
import top.codervan.yygh.model.hosp.Department;
import top.codervan.yygh.model.hosp.Hospital;
import top.codervan.yygh.model.hosp.Schedule;
import top.codervan.yygh.vo.hosp.DepartmentQueryVo;
import top.codervan.yygh.vo.hosp.ScheduleQueryVo;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: van
 * @Date: 2022/2/18 15:25
 */
@Api("医院管理添加")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 查询医院接口
     * @param request 医院平台发送过来的数据
     * @return 返回保存结果
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来医院信息,进行转换
        Map<String, Object> paramMap = HospApiUtil.toParamMap(request);
        //进行签名判断
        boolean result = HospApiUtil.checkSign(paramMap,hospitalSetService);
        if(!result){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service里面的方法根据编号进行查询
        String hoscode = (String)paramMap.get("hoscode");
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);

    }
    /**
     * 上传医院接口
     * @param request 医院平台发送过来的数据
     * @return 返回保存结果
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来医院信息,进行转换
        Map<String, Object> paramMap = HospApiUtil.toParamMap(request);
        //进行签名判断
        boolean result = HospApiUtil.checkSign(paramMap,hospitalSetService);
        if(!result){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+”转换为了“ ” ，所以我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData",logoData);
        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    /**
     * 上传科室接口
     * @param request
     * @return
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来科室信息,进行转换
        Map<String, Object> paramMap = HospApiUtil.toParamMap(request);
        //进行签名判断
        boolean result = HospApiUtil.checkSign(paramMap,hospitalSetService);
        if(!result){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * 查询科室接口
     * @param request
     * @return
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传递过来科室信息,进行转换
        Map<String, Object> paramMap = HospApiUtil.toParamMap(request);
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //当前页 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page"))?1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1 : Integer.parseInt((String)paramMap.get("limit"));
        //进行签名判断
        boolean result = HospApiUtil.checkSign(paramMap,hospitalSetService);
        if(!result){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除科室接口
     * @param request
     * @return
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来科室信息,进行转换
        Map<String, Object> paramMap = HospApiUtil.toParamMap(request);
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //科室编号
        String depcode = (String)paramMap.get("depcode");
        //进行签名判断
        boolean result = HospApiUtil.checkSign(paramMap,hospitalSetService);
        if(!result){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    /**
     * 添加排班接口
     * @param request
     * @return
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来排班信息,进行转换
        Map<String, Object> paramMap = HospApiUtil.toParamMap(request);
        //进行签名判断
        boolean result = HospApiUtil.checkSign(paramMap,hospitalSetService);
        if(!result){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 查询排班接口
     * @param request
     * @return
     */
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递过来排班信息,进行转换
        Map<String, Object> paramMap = HospApiUtil.toParamMap(request);
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //科室编号
        String depcode = (String)paramMap.get("depcode");
        //当前页 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page"))?1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1 : Integer.parseInt((String)paramMap.get("limit"));
        //进行签名判断
        boolean result = HospApiUtil.checkSign(paramMap,hospitalSetService);
        if(!result){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除排班接口
     * @param request
     * @return
     */
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取传递过来科室信息,进行转换
        Map<String, Object> paramMap = HospApiUtil.toParamMap(request);
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //科室编号
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        //进行签名判断
        boolean result = HospApiUtil.checkSign(paramMap,hospitalSetService);
        if(!result){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
}
